package com.woniuxy.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.coupon.client.CouponQueryClient;
import com.woniuxy.coupon.client.result.CouponDto;
import com.woniuxy.member.infra.mapper.MemberCouponMapper;
import com.woniuxy.member.infra.model.MemberCoupon;
import com.woniuxy.member.service.MemberCouponService;
import com.woniuxy.member.service.dto.MemberCouponDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.List;

/**
 * <p>
 * 用户关联优惠卷表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class MemberCouponServiceImpl extends ServiceImpl<MemberCouponMapper, MemberCoupon> implements MemberCouponService {
    @Autowired
    private MemberCouponMapper memberCouponMapper;
    @Autowired
    private CouponQueryClient couponQueryClient;
    @Override
    public List<MemberCouponDto> getMemberCoupon(Integer memberId,Integer storeId) {
        QueryWrapper<MemberCoupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        List<MemberCoupon> memberCoupons = memberCouponMapper.selectList(queryWrapper);
        List<MemberCouponDto> memberCouponDtos = BeanUtil.copyToList(memberCoupons, MemberCouponDto.class);
        for (MemberCouponDto memberCoupon : memberCouponDtos) {
            Result<CouponDto> couponDtoResult = couponQueryClient.queryCouponById(memberCoupon.getCouponId());
            CouponDto couponDto = couponDtoResult.getData();
            if (couponDto.getCouponState().equals("OFF")){
                memberCoupon.setUseState("OFF");
            }
            memberCoupon.setCouponName(couponDto.getCouponName());
            memberCoupon.setCouponFace(couponDto.getCouponFace());
        }



        return memberCouponDtos;
    }
}
